﻿/* 2011/8/1 */
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Collections;
using System.Collections.ObjectModel;

namespace PictureManager.ClassifyFile
{
    /// <summary>
    /// A theItem which is able to be added to ListView. It contains a CorrespondenceRule.
    /// </summary>
    class CorrespondenceRuleItem : ListViewItem
    {
        private CorrespondenceRule _rule;

        public CorrespondenceRuleItem(string fileNamePattern, string folderPath, bool isSubFolder, CorrespondOption correspondOption)
        {
            _rule = new CorrespondenceRule(fileNamePattern, folderPath, isSubFolder, correspondOption);

            SubItems[0].Text = FileNameCategory.GetOptionDescription(_rule.CorrespondOption);
            SubItems.Add(_rule.FileNamePattern);
            SubItems.Add(_rule.FolderPath);
            SubItems.Add(_rule.IsSubFolder.ToString());
        }

        public CorrespondenceRuleItem(string escapeString)
        {
            _rule = new CorrespondenceRule(escapeString);

            SubItems[0].Text = FileNameCategory.GetOptionDescription(_rule.CorrespondOption);
            SubItems.Add(_rule.FileNamePattern);
            SubItems.Add(_rule.FolderPath);
            SubItems.Add(_rule.IsSubFolder.ToString());
        }

        public CorrespondenceRule Rule
        {
            get { return _rule; }
        }

        public string FileNamePattern
        {
            get { return _rule.FileNamePattern; }
            set { _rule.FileNamePattern = value; SubItems[1].Text = _rule.FileNamePattern; }
        }

        public string FolderPath
        {
            get { return _rule.FolderPath; }
            set { _rule.FolderPath = value; SubItems[2].Text = _rule.FolderPath; }
        }

        public bool IsSubFolder
        {
            get { return _rule.IsSubFolder; }
            set { _rule.IsSubFolder = value; SubItems[3].Text = _rule.IsSubFolder.ToString(); }
        }

        public CorrespondOption CorrespondOption
        {
            get { return _rule.CorrespondOption; }
            set { _rule.CorrespondOption = value; SubItems[0].Text = FileNameCategory.GetOptionDescription(_rule.CorrespondOption); }
        }

        public string EscapeString
        {
            get { return _rule.EscapeString; }
        }
    }

    class CorrespondenceRuleItemSorter : IComparer
    {
        public enum SortOption
        {
            Pattern,
            Folder
        }

        private SortOption _option;
        private SortOrder _order;

        public CorrespondenceRuleItemSorter()
            : this(SortOption.Folder, SortOrder.Ascending)
        {
        }

        public CorrespondenceRuleItemSorter(SortOption option)
            : this(option, SortOrder.Ascending)
        {
        }

        public CorrespondenceRuleItemSorter(SortOption option, SortOrder order)
        {
            _option = option;
            _order = order;
        }

        public int Compare(object first, object second)
        {
            CorrespondenceRuleItem firstItem = (CorrespondenceRuleItem)first;
            CorrespondenceRuleItem secondItem = (CorrespondenceRuleItem)second;
            int result = 0;
            switch (_option)
            {
                case SortOption.Pattern:
                    if (_order == SortOrder.Ascending)
                        result = String.Compare(firstItem.FileNamePattern, secondItem.FileNamePattern);
                    else
                        result = String.Compare(secondItem.FileNamePattern, firstItem.FileNamePattern);
                    break;
                case SortOption.Folder:
                    if (_order == SortOrder.Ascending)
                        result = String.Compare(firstItem.FolderPath, secondItem.FolderPath);
                    else
                        result = String.Compare(secondItem.FolderPath, firstItem.FolderPath);
                    break;
                default:
                    break;
            }
            return result;
        }

        public void ReverseOrder()
        {
            if (_order == SortOrder.Ascending)
                _order = SortOrder.Descending;
            else
                _order = SortOrder.Ascending;
        }

        public SortOption Option
        {
            get { return _option; }
        }

        public SortOrder Order
        {
            get { return _order; }
        }
    }      
}
